package com.example.stock.base;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.stock.util.LoadingDialogUtil;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

public abstract class BaseFragment<D extends ViewDataBinding, T extends BaseViewModel> extends LazyFragment {

    protected D mBinding;
    protected T mViewModel;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Class<T> tClassModel = initViewModel();
        if (tClassModel != null) {
            mViewModel = new ViewModelProvider(this).get(tClassModel);
        }
        mBinding = DataBindingUtil.inflate(inflater, getLayoutId(), container, false);
        registerObserve();
        return mBinding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    protected abstract Class<T> initViewModel();

    protected abstract int getLayoutId();

    protected abstract void initView();

    protected abstract void lazyInit();

    public void visible() {

    }

    protected void registerObserve() {
        if (mViewModel == null) return;
        mViewModel.getShowDialog().observe(getViewLifecycleOwner(), new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if (aBoolean) {
                    showLoadingDialog("");
                } else {
                    hideLoadingDialog();
                }
            }
        });
        mViewModel.getDialogMsg().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(String s) {
                if (!TextUtils.isEmpty(s)) {
                    showLoadingDialog(s);
                } else {
                    hideLoadingDialog();
                }
            }
        });
    }


    private LoadingDialogUtil mLoadingDialog;

    public void showLoadingDialog(String title) {
        if (mLoadingDialog == null) {
            mLoadingDialog = LoadingDialogUtil.getInstance();

        }
        if (!mLoadingDialog.isShowing()) {
            mLoadingDialog.showLoadingDialog(getContext(), TextUtils.isEmpty(title) ?  "Loading..." : title);//开启加载动画
        }
    }

    public void hideLoadingDialog() {
        if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
            mLoadingDialog.closeLoadingDialog();
            mLoadingDialog = null;
        }
    }

    @Override
    public void onDestroyView() {
        hideLoadingDialog();
        super.onDestroyView();
    }
}
